package cn.itcast.Controller;

import cn.itcast.Service.EmployeeService;
import cn.itcast.domain.Employee;
import cn.itcast.domain.Msg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;

    @RequestMapping("/emps")
    @ResponseBody
    public Msg getEmpsWithJson(@RequestParam(value = "pn",defaultValue = "1") Integer pn ){
        PageHelper.startPage(pn,5);
        List<Employee> employees = employeeService.getAll();
        //分页查询
        PageInfo page = new PageInfo(employees,5);

        return Msg.success().add("pageInfo",page);
    }

    //保存员工
    @RequestMapping(value = "/emp",method = RequestMethod.POST)
    @ResponseBody
    public Msg saveEmp(@Valid Employee employee, BindingResult result){

        Map<String ,Object> map = new HashMap<String,Object>();

        if (result.hasErrors()){
            List<FieldError> fieldErrors = result.getFieldErrors();
            for (FieldError fieldError : fieldErrors) {
                String field = fieldError.getField();
                String defaultMessage = fieldError.getDefaultMessage();
                map.put(field,defaultMessage);

            }
            return Msg.fail().add("errorMap",map);
        }else {

            employeeService.save(employee);
            return Msg.success();
        }

    }
    //查询员工

    @RequestMapping(value = "/emp/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Msg getEmp(@PathVariable("id") Integer id){
        Employee employee = employeeService.getEmp(id);

        return Msg.success().add("emp",employee);
    }
    //修改员工
    @RequestMapping(value = "/emp/{empId}",method = RequestMethod.PUT)
    @ResponseBody
    public Msg updateEmp(Employee employee){
        System.out.println(employee);
        employeeService.updateEmp(employee);

        return Msg.success();
    }
   /* //删除员工
    @RequestMapping(value = "/emp/{id}" ,method = RequestMethod.DELETE)
    @ResponseBody
    public Msg deleteEmp(@PathVariable("id") Integer id){
        employeeService.deleteEmp(id);
        return Msg.success();
    }*/
    //删除员工-plus
    @RequestMapping(value = "/emp/{ids}" ,method = RequestMethod.DELETE)
    @ResponseBody
    public Msg deleteEmpPlus(@PathVariable("ids") String ids){
        if (ids.contains("-")){
            List<Integer> id_list  =new ArrayList<Integer>();
            String[] s_ids = ids.split("-");
            for (String s_id : s_ids) {
                id_list.add(Integer.parseInt(s_id));
            }
            employeeService.deleteBatch(id_list);

        }else {
            int id = Integer.parseInt(ids);
            employeeService.deleteEmp(id);

        }
        return Msg.success();


    }
    //校验用户名是否重复
    @ResponseBody
    @RequestMapping("/checkUser")
    public Msg checked(@RequestParam(value = "empName") String empName){
        //后端校验
        String regx = "(^[a-zA-Z0-9_-]{3,16}$)|(^[\\u2E80-\\u9FFF]{2,5})";
        if (!empName.matches(regx)){
            return Msg.fail().add("va_msg","后端校验不通过");
        }

        //数据库校验
       boolean b = employeeService.checkUser(empName);
       if (b){
           return Msg.success();
       }else {
           return Msg.fail().add("va_msg","用户名不可用");
       }



    }

}
